package com.bjb.vr.manager.aop;

import com.bjb.vr.common.ao.UmsAccountAo;
import com.bjb.vr.common.constant.LoginConstant;
import com.bjb.vr.common.constant.ThreadLocalConstant;
import com.bjb.vr.common.constant.TokenConstants;
import com.bjb.vr.common.exception.CommonException;
import com.bjb.vr.common.result.BaseErrorCode;
import com.bjb.vr.common.result.CommonResult;
import com.bjb.vr.common.service.OptCusConfigService;
import com.bjb.vr.common.service.UmsAccountService;
import com.bjb.vr.common.service.UmsOptAccountService;
import com.bjb.vr.common.utils.JwtTokenUtil;
import com.bjb.vr.common.utils.ThreadLocalUtil;
import com.bjb.vr.common.vo.UmsAccountVo;
import com.bjb.vr.manager.utils.AccessRequestUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

/**
 * 请求信息拦截器--这样不用在每个请求方法上都校验一遍请求头中的token，提取出来
 *
 * @version 1.0.0
 * @author: HuJingBo
 * @time: 2022/4/18 11:37
 */
@Slf4j
@Aspect
@Component
public class UserAccoutInfoAspect {
    @Autowired
    private UmsAccountService accountService;
    @Resource
    private UmsOptAccountService optAccountService;

    @Resource
    private OptCusConfigService optCusConfigService;


    @Pointcut(value = "execution(* com.bjb.vr.manager.controller.*.*(..))")
    public void checkUserPointcut() {
    }

    @Around(value = "checkUserPointcut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        Map<String, String> headers = AccessRequestUtil.takeHeaders();
        // 获取token
        if (!headers.containsKey(TokenConstants.AUTHORIZATION.toLowerCase())) {
            throw new CommonException("请求头中没有token");
        }

        // 查询用户信息
        UmsAccountAo umsAccountAo = UmsAccountAo.builder().authorization(headers.get(TokenConstants.AUTHORIZATION.toLowerCase())).build();
        Claims claims = JwtTokenUtil.parseJWT(umsAccountAo.getAuthorization());
        String loginType = String.valueOf(claims.get(TokenConstants.LOGIN_TYPE));
        UmsAccountVo umsAccountVo = null;
        if (LoginConstant.MANAGER.equals(loginType)) {
            umsAccountVo = accountService.getAccountInfo(umsAccountAo);
            // 查询平台版本
            optCusConfigService.queryChoicedPlatform(umsAccountAo);
        }else {
            umsAccountVo = optAccountService.getAccountInfo(umsAccountAo);
        }

        // 判断是否为空
        if (Objects.isNull(umsAccountVo)) {
            return CommonResult.Fail(BaseErrorCode.ERROR_AUTHENTICATION);
        }

        Object resultObject = null;
        try {
            resultObject = pjp.proceed();
        } catch (Exception ex) {
            throw ex;
        } finally {
            // 清除线程变量
            ThreadLocalUtil.remove(ThreadLocalConstant.USER_ACCOUNT_INFO);
            ThreadLocalUtil.remove(ThreadLocalConstant.NO_TENANT_ID_VERIFY);
            ThreadLocalUtil.remove(ThreadLocalConstant.USER_PLATFORM_INFO);
        }
        return resultObject;
    }
}
